package com.putong35.common.baseactfrag.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2017/7/20.
 */

public class DataBindRecyclerViewAdapter<T> extends RecyclerViewBaseAdapter<T> {

    private @LayoutRes
    int itemLayout;

    private int brItemBean;

    public DataBindRecyclerViewAdapter(Context con, @LayoutRes int itemLayout, int brItemBean) {
        super(con);
        this.itemLayout = itemLayout;
        this.brItemBean = brItemBean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int i) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater
                .from(viewGroup.getContext()), itemLayout, viewGroup, false);
        DataBindViewHolder holder = new DataBindViewHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }


    @Override
    public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
        DataBindViewHolder holder = (DataBindViewHolder) viewHolder;
        holder.getBinding().setVariable(brItemBean, getData().get(i));
        holder.getBinding().executePendingBindings();
    }

}
